package com.xbongbong.saas.enums;

import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import static com.xbongbong.saas.enums.RelativeProductEnum.ATTRITION_RATE;
import static com.xbongbong.saas.enums.RelativeProductEnum.BARCODE;
import static com.xbongbong.saas.enums.RelativeProductEnum.BATCH;
import static com.xbongbong.saas.enums.RelativeProductEnum.COST;
import static com.xbongbong.saas.enums.RelativeProductEnum.GUARANTEE_PERIOD;
import static com.xbongbong.saas.enums.RelativeProductEnum.MEMO;
import static com.xbongbong.saas.enums.RelativeProductEnum.NAME;
import static com.xbongbong.saas.enums.RelativeProductEnum.NUM;
import static com.xbongbong.saas.enums.RelativeProductEnum.PRODUCE_DATE;
import static com.xbongbong.saas.enums.RelativeProductEnum.PRODUCT_NO;
import static com.xbongbong.saas.enums.RelativeProductEnum.PRODUCT_PRICE;
import static com.xbongbong.saas.enums.RelativeProductEnum.SALE_DISCOUNT;
import static com.xbongbong.saas.enums.RelativeProductEnum.SALE_PRODUCT_PRICE;
import static com.xbongbong.saas.enums.RelativeProductEnum.SALE_SUBTOTAL;
import static com.xbongbong.saas.enums.RelativeProductEnum.SPECIFICATION;
import static com.xbongbong.saas.enums.RelativeProductEnum.STOCK;
import static com.xbongbong.saas.enums.RelativeProductEnum.SUBTOTAL;
import static com.xbongbong.saas.enums.RelativeProductEnum.THUMBNAIL;
import static com.xbongbong.saas.enums.RelativeProductEnum.UNIT;
import static com.xbongbong.saas.enums.RelativeProductEnum.UNIT_NUM;
import static com.xbongbong.saas.enums.RelativeProductEnum.WAREHOUSE;


/**
 * 业务关联产品展示字段枚举类
 *
 * @author chaoxiong.lei
 * @date 2019/1/14 13:57
 */
@Getter
public enum BusinessProductEnum {
    // 产品需要展示的字段
    BOM(XbbRefTypeEnum.BOM_BILL.getCode(), XbbRefTypeEnum.BOM_BILL.getCode(), Arrays.asList(THUMBNAIL,NAME, PRODUCT_NO, SPECIFICATION, BARCODE, NUM, UNIT, ATTRITION_RATE)),
    // 模拟生产-开始模拟，物料返回的字段数据
    BOM_SIMULATED_PRODUCTION(XbbRefTypeEnum.BOM_BILL.getCode(), XbbRefTypeEnum.SIMULATED_PRODUCTION.getCode(), Arrays.asList(NAME, PRODUCT_NO, SPECIFICATION, WAREHOUSE, STOCK, UNIT_NUM, UNIT, ATTRITION_RATE)),

    SIMULATED_PRODUCTION(XbbRefTypeEnum.PRODUCTION_ORDER.getCode(), XbbRefTypeEnum.SIMULATED_PRODUCTION.getCode(), Arrays.asList(NAME, PRODUCT_NO, SPECIFICATION, WAREHOUSE, STOCK, UNIT_NUM, UNIT, ATTRITION_RATE, THUMBNAIL)),

    INVENTORY(XbbRefTypeEnum.INVENTORY.getCode(), XbbRefTypeEnum.INVENTORY.getCode(), Arrays.asList(THUMBNAIL,NAME, PRODUCT_NO, SPECIFICATION, BARCODE, NUM, STOCK, UNIT, BATCH, GUARANTEE_PERIOD, PRODUCE_DATE, MEMO)),
    TRANSFER(XbbRefTypeEnum.TRANSFER.getCode(), XbbRefTypeEnum.TRANSFER.getCode(), Arrays.asList(THUMBNAIL,NAME, PRODUCT_NO, SPECIFICATION, BARCODE, NUM, STOCK, UNIT, BATCH, GUARANTEE_PERIOD, PRODUCE_DATE, MEMO)),
    REFUND(XbbRefTypeEnum.REFUND.getCode(), XbbRefTypeEnum.REFUND.getCode(), Arrays.asList(NAME, PRODUCT_NO, SPECIFICATION, PRODUCT_PRICE, NUM, MEMO, UNIT)),
    PRODUCTION_ORDER(XbbRefTypeEnum.PRODUCTION_ORDER.getCode(), XbbRefTypeEnum.PRODUCTION_ORDER.getCode(), Arrays.asList(THUMBNAIL,NAME, PRODUCT_NO, SPECIFICATION, BARCODE, WAREHOUSE, UNIT_NUM, NUM, STOCK, UNIT, ATTRITION_RATE)),
    SUPPLIER(XbbRefTypeEnum.SUPPLIER.getCode(), XbbRefTypeEnum.SUPPLIER.getCode(),  Arrays.asList(NAME, PRODUCT_NO,UNIT)),
    OPPORTUNITY(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), Arrays.asList(THUMBNAIL, NAME, PRODUCT_NO, SPECIFICATION, BARCODE, PRODUCT_PRICE, SALE_DISCOUNT, SALE_PRODUCT_PRICE, NUM, UNIT, SUBTOTAL)),
    OUTSTOCK(XbbRefTypeEnum.OUTSTOCK.getCode(), XbbRefTypeEnum.OUTSTOCK.getCode(),  Arrays.asList(THUMBNAIL,NAME, PRODUCT_NO, SPECIFICATION, BARCODE, WAREHOUSE, NUM, COST, STOCK, UNIT, BATCH, GUARANTEE_PERIOD, PRODUCE_DATE, MEMO)),

    CONTRACT_OUTSTOCK(XbbRefTypeEnum.OUTSTOCK.getCode(), XbbRefTypeEnum.CONTRACT.getCode(),  Arrays.asList(THUMBNAIL,NAME, PRODUCT_NO, SPECIFICATION, BARCODE, WAREHOUSE, NUM, COST, STOCK, UNIT, BATCH, GUARANTEE_PERIOD, PRODUCE_DATE, MEMO, SALE_PRODUCT_PRICE, SALE_SUBTOTAL, PRODUCT_PRICE, SALE_DISCOUNT)),

    INVENTORY_OUTSTOCK(XbbRefTypeEnum.OUTSTOCK.getCode(), XbbRefTypeEnum.INVENTORY.getCode(),  Arrays.asList(THUMBNAIL,NAME, PRODUCT_NO, SPECIFICATION, BARCODE, WAREHOUSE, NUM, COST, STOCK, UNIT, BATCH, GUARANTEE_PERIOD, PRODUCE_DATE, MEMO)),
    TRANSFER_OUTSTOCK(XbbRefTypeEnum.OUTSTOCK.getCode(), XbbRefTypeEnum.TRANSFER.getCode(),  Arrays.asList(THUMBNAIL,NAME, PRODUCT_NO, SPECIFICATION, BARCODE, WAREHOUSE, NUM, COST, STOCK, UNIT, BATCH, GUARANTEE_PERIOD, PRODUCE_DATE, MEMO)),
    BOM_BILL_OUTSTOCK(XbbRefTypeEnum.OUTSTOCK.getCode(), XbbRefTypeEnum.BOM_BILL.getCode(),  Arrays.asList(NAME, PRODUCT_NO, SPECIFICATION, BARCODE, WAREHOUSE, NUM, COST,  STOCK, UNIT, BATCH, GUARANTEE_PERIOD, PRODUCE_DATE, MEMO)),
    PRODUCTION_ORDER_OUTSTOCK(XbbRefTypeEnum.OUTSTOCK.getCode(), XbbRefTypeEnum.PRODUCTION_ORDER.getCode(),  Arrays.asList(THUMBNAIL,NAME, PRODUCT_NO, SPECIFICATION, BARCODE, WAREHOUSE, NUM, COST,  STOCK, UNIT, BATCH, GUARANTEE_PERIOD, PRODUCE_DATE, MEMO)),
    // 出库单-出库类型选择采购退货出库-关联产品的字段
    PURCHASE_OUTSTOCK(XbbRefTypeEnum.OUTSTOCK.getCode(), XbbRefTypeEnum.PURCHASE.getCode(),  Arrays.asList(NAME, PRODUCT_NO, SPECIFICATION, BARCODE, WAREHOUSE, NUM, COST, STOCK, UNIT, BATCH, GUARANTEE_PERIOD, PRODUCE_DATE, MEMO)),
    RETURNED_PURCHASE_OUTSTOCK(XbbRefTypeEnum.OUTSTOCK.getCode(), XbbRefTypeEnum.RETURNED_PURCHASE.getCode(),  Arrays.asList(THUMBNAIL,NAME, PRODUCT_NO, SPECIFICATION, BARCODE, WAREHOUSE, NUM, COST, STOCK, UNIT, BATCH, GUARANTEE_PERIOD, PRODUCE_DATE, MEMO)),
    SUPPLIER_OUTSTOCK(XbbRefTypeEnum.OUTSTOCK.getCode(), XbbRefTypeEnum.SUPPLIER.getCode(),  Arrays.asList(NAME, PRODUCT_NO, SPECIFICATION, BARCODE, WAREHOUSE, NUM, COST, STOCK, UNIT, BATCH, GUARANTEE_PERIOD, PRODUCE_DATE, MEMO)),
    ASSEMBLE_OUTSTOCK(XbbRefTypeEnum.OUTSTOCK.getCode(), XbbRefTypeEnum.ASSEMBLE.getCode(),  Arrays.asList(NAME, PRODUCT_NO, SPECIFICATION, BARCODE, WAREHOUSE, NUM, COST, STOCK, UNIT, BATCH, GUARANTEE_PERIOD, PRODUCE_DATE, MEMO)),
    SALES_OPPORTUNITY_OUTSTOCK(XbbRefTypeEnum.OUTSTOCK.getCode(), XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(),  Arrays.asList(NAME, PRODUCT_NO, SPECIFICATION, BARCODE, WAREHOUSE, NUM, COST, STOCK, UNIT, BATCH, GUARANTEE_PERIOD, PRODUCE_DATE, MEMO)),


    INSTOCK(XbbRefTypeEnum.INSTOCK.getCode(), XbbRefTypeEnum.INSTOCK.getCode(),  Arrays.asList(THUMBNAIL,NAME, PRODUCT_NO, SPECIFICATION, BARCODE, WAREHOUSE, NUM, COST, STOCK, UNIT, BATCH, GUARANTEE_PERIOD, PRODUCE_DATE, MEMO)),
    INSTOCK_CONTRACT(XbbRefTypeEnum.INSTOCK.getCode(),XbbRefTypeEnum.CONTRACT.getCode(), Arrays.asList(NAME, PRODUCT_NO, SPECIFICATION, WAREHOUSE, NUM, COST, STOCK, UNIT, BATCH, GUARANTEE_PERIOD, PRODUCE_DATE, MEMO)),


    CONTRACT(XbbRefTypeEnum.CONTRACT.getCode(), XbbRefTypeEnum.CONTRACT.getCode(),  Arrays.asList(THUMBNAIL,NAME, PRODUCT_NO, SPECIFICATION, BARCODE, PRODUCT_PRICE, SALE_DISCOUNT, SALE_PRODUCT_PRICE, NUM, MEMO, UNIT, SUBTOTAL)),
    PURCHASE(XbbRefTypeEnum.PURCHASE.getCode(), XbbRefTypeEnum.PURCHASE.getCode(), Arrays.asList(NAME,PRODUCT_NO,SPECIFICATION,BARCODE,NUM,PRODUCT_PRICE,MEMO,SUBTOTAL,UNIT)),
    RETURNED_PURCHASE(XbbRefTypeEnum.RETURNED_PURCHASE.getCode(), XbbRefTypeEnum.RETURNED_PURCHASE.getCode(), Arrays.asList(NAME,PRODUCT_NO,BARCODE,NUM,PRODUCT_PRICE,MEMO,SUBTOTAL,UNIT)),
    ASSEMBLE(XbbRefTypeEnum.ASSEMBLE.getCode(), XbbRefTypeEnum.ASSEMBLE.getCode(), Arrays.asList(NAME,PRODUCT_NO,SPECIFICATION,BARCODE,WAREHOUSE,NUM,COST,STOCK,UNIT,PRODUCE_DATE,GUARANTEE_PERIOD,BATCH,MEMO)),
    WORK_ORDER(XbbRefTypeEnum.WORK_ORDER.getCode(), XbbRefTypeEnum.WORK_ORDER.getCode(), Arrays.asList(NAME, PRODUCT_NO,SPECIFICATION ,PRODUCT_PRICE,NUM , UNIT)),
    QUOTATION(XbbRefTypeEnum.QUOTATION.getCode(), XbbRefTypeEnum.QUOTATION.getCode(), Arrays.asList(THUMBNAIL, NAME, PRODUCT_NO,SPECIFICATION ,BARCODE,PRODUCT_PRICE,SALE_DISCOUNT,SALE_PRODUCT_PRICE,NUM,MEMO,UNIT,SUBTOTAL)),

    FULL_DISCOUNT_COUPON(XbbRefTypeEnum.FULL_DISCOUNT_COUPON.getCode(), XbbRefTypeEnum.FULL_DISCOUNT_COUPON.getCode(), Arrays.asList(THUMBNAIL, NAME, PRODUCT_NO,SPECIFICATION ,BARCODE,PRODUCT_PRICE,SALE_DISCOUNT,SALE_PRODUCT_PRICE,NUM,MEMO,UNIT,SUBTOTAL)),
    SPECIAL_GOODS(XbbRefTypeEnum.SPECIAL_GOODS.getCode(), XbbRefTypeEnum.SPECIAL_GOODS.getCode(), Arrays.asList(THUMBNAIL, NAME, PRODUCT_NO,SPECIFICATION ,BARCODE,PRODUCT_PRICE,SALE_DISCOUNT,SALE_PRODUCT_PRICE,NUM,MEMO,UNIT,SUBTOTAL)),
    COST_ADJUST(XbbRefTypeEnum.COST_ADJUST.getCode(), XbbRefTypeEnum.COST_ADJUST.getCode(), Arrays.asList(THUMBNAIL, NAME, PRODUCT_NO,SPECIFICATION ,BARCODE,PRODUCT_PRICE,SALE_DISCOUNT,SALE_PRODUCT_PRICE,NUM,MEMO,UNIT,SUBTOTAL)),

    WORK_ORDER_V2(XbbRefTypeEnum.WORK_ORDER_V2.getCode(), XbbRefTypeEnum.WORK_ORDER_V2.getCode(), Arrays.asList(NAME, PRODUCT_NO,SPECIFICATION ,BARCODE,PRODUCT_PRICE,SALE_DISCOUNT,SALE_PRODUCT_PRICE,NUM,MEMO,UNIT,SUBTOTAL)),

    UNKNOW(null, null, Arrays.asList()),
    ;

    /**
     * 对应xbbRefType中的code
     */
    Integer code;

    /**
     * 用来判断是显示在哪个业务的
     */
    Integer targetCode;

    /**
     * 需要展示的字段集合
     */
    List<RelativeProductEnum> list;

    BusinessProductEnum(Integer code, Integer targetCode, List<RelativeProductEnum> list) {
        this.code = code;
        this.targetCode = targetCode;
        this.list = list;
    }

    /**
     * 获取全部枚举
     *
     * @return List<RelativeProductEnum>
     */
    public static List<BusinessProductEnum> getAllEnum() {
        List<BusinessProductEnum> list = new ArrayList<>();
        for (BusinessProductEnum cache : values()) {
            list.add(cache);
        }
        return list;
    }

    /**
     * 根据code获取对应枚举
     *
     * @return RelativeProductEnum
     */
    public static BusinessProductEnum getByCodeAndTargetCode(Integer code, Integer targetCode) {
        if (code == null) {
            return UNKNOW;
        }
        for (BusinessProductEnum cache : values()) {
            if (Objects.equals(cache.getCode(), code) && Objects.equals(cache.getTargetCode(), targetCode)) {
                return cache;
            }
        }
        return UNKNOW;
    }

    /**
     * 根据code获取对应枚举
     *
     * @return RelativeProductEnum
     */
    public static BusinessProductEnum getByCode(Integer code) {
        if (code == null) {
            return UNKNOW;
        }
        for (BusinessProductEnum cache : values()) {
            if (Objects.equals(cache.getCode(), code) && Objects.equals(cache.getTargetCode(), code)) {
                return cache;
            }
        }
        return UNKNOW;
    }
}
